package net.sahv.bdyz.api;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;

import net.sahv.bdyz.controller.web.PatternController;
import net.sahv.bdyz.model.Fabric;
import net.sahv.bdyz.model.FabricFilter;
import net.sahv.bdyz.model.HelpCenter;
import net.sahv.bdyz.model.Member;
import net.sahv.bdyz.model.Pattern;
import net.sahv.bdyz.model.PatternFilter;
import net.sahv.bdyz.model.PatternPrint;
import net.sahv.bdyz.service.FabricFilterService;
import net.sahv.bdyz.service.FabricService;
import net.sahv.bdyz.service.HelpCenterService;
import net.sahv.bdyz.service.LanguageService;
import net.sahv.bdyz.service.MemberService;
import net.sahv.bdyz.service.PatternFilterService;
import net.sahv.bdyz.service.PatternPrintService;
import net.sahv.bdyz.service.PatternService;
import net.sahv.bdyz.util.LoadCfgUtil;
import net.sahv.bdyz.util.PageInfo;

@Controller
@RequestMapping("api/pattern/")
public class PatternApiController {
	@Autowired
	private HelpCenterService helpCenterService;
	@Autowired
	private MemberService memberService;
	@Autowired
	private PatternFilterService patternFilterService;
	@Autowired
	private PatternService patternService;
	@Autowired
	private FabricService fabricService;
	@Autowired
	private PatternPrintService patternPrintService;
	@Autowired
	private LanguageService languageService;// 语言
	@Autowired
	private FabricFilterService fabricFilterService;
	
	@Autowired 
	CookieLocaleResolver resolver;
	@RequestMapping(value = "{canshu}/flowertypecatalog{page}", method = RequestMethod.GET)
	public String doposition(HttpServletRequest request,HttpServletResponse response,Model model, @PathVariable(value = "canshu") String canshu, @PathVariable(value = "page") String page) throws FileNotFoundException, IOException {
		Object languageObj=request.getSession().getAttribute("language");
		
		Map<String, String> mapUrls = LoadCfgUtil.loadCfg();
		
		model.addAttribute("imgpath",mapUrls.get("targetUrl.img"));
		
		
		 String yuyan="";
		if(languageObj!=null){
			String language=String.valueOf(languageObj);
			if(language!=null&&!language.equals("")){
				language=language.toLowerCase();
				if(language.equals("zh_cn")){
					yuyan="ch";
					resolver.setLocale(request, response, Locale.CHINA );
				}else if(language.equals("en")){
					yuyan="en";
					resolver.setLocale(request, response, Locale.ENGLISH );
				}else{
					yuyan="ch";
					resolver.setLocale(request, response, Locale.CHINA );
				}
			}
		}else{
			yuyan="ch";
		}
		model.addAttribute("yuyan", yuyan);
		Member member = memberService.getCurrent();//获取当前登录人信息
		if(canshu.indexOf("02")>=0){
		if(member==null){
			return "redirect:/api/pattern/all/flowertypecatalog1";
		}else{
			if(member.getmVip()==0){//不是vip
				return "redirect:/api/pattern/all/flowertypecatalog1";
				
			}
		}
		}
		List<Map<String, Object>> zong = new ArrayList<>();
		List<PatternFilter> list = patternFilterService.selectAllBypid(0,yuyan);
		for (PatternFilter item : list) {
			Map<String, Object> map = new HashMap<String, Object>();
			List<PatternFilter> zilist = patternFilterService.selectAllBypid(item.getPtId(),yuyan);
			map.put("fuji", item);
			map.put("zi", zilist);
			zong.add(map);
		}
		model.addAttribute("shuju", zong);
		model.addAttribute("canshu", canshu);

		// 查询
		PageInfo pageInfo = new PageInfo();
		if(page.equals("")){
			page="1";
		}
		pageInfo.setPage(Integer.valueOf(page));
		pageInfo.setPageSize(10);
		
		List<Map<String, Object>> pattern = patternService.findAll(pageInfo, canshu, " ORDER BY ",member);
		model.addAttribute("pattern", pattern);
		model.addAttribute("pageInfo", pageInfo);
		model.addAttribute("member", member);
		model.addAttribute("parambottom", "2");				
		return "phone/pattern/pattern";
	}
	
	
	
	@RequestMapping(value = "viewpattern", method = RequestMethod.GET)
	public String viewpattern(HttpServletRequest request,String pNo) {
		Pattern pattern =patternService.selectByNo(pNo);
		return "redirect:/api/toPhonePage/patternShiyi?id="+pattern.getpId();
	}
	
	@RequestMapping(value = "viewf", method = RequestMethod.GET)
	public String viewf(HttpServletRequest request,String fNo) {
		Fabric fabric =fabricService.selectByNo(fNo);
		return "redirect:/api/fabric/detail?fId="+fabric.getfId();
	}
	
	/**
	 * 成品下单界面
	 * @author 张天敏
	 * @date 日期：2016年10月7日 时间：上午9:49:13
	 * @param model
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	@RequestMapping(value="/chengpin",method=RequestMethod.GET)
	public String chengpin(Model model,Integer id,HttpServletRequest request,HttpServletResponse response) throws FileNotFoundException, IOException{
		String language = languageService.getCurrentLanguage();
		
		List<Fabric> fabricListFIngredientsList=new ArrayList<Fabric>();
		
		//查询花型
		Pattern pattern =  patternService.selectByPrimaryKey(id);
		List<Pattern> patternlist =  patternService.findAllBylimit(pattern);
		//查询成分
		List<FabricFilter> fabricFilterList=fabricFilterService.selectByFfParentId(1);
		
		for(FabricFilter fabricFilter:fabricFilterList){
			//查询成分面料
			List<Fabric> fabricList=fabricService.selectByFIngredients(fabricFilter.getFfId());
			for(Fabric fabric:fabricList){
				//如果是英文页面，则将英文内容覆盖中文内容
				if (language != null && language.equals("en")){
					fabric.setfName(fabric.getfMessage1());
				}
				fabricListFIngredientsList.add(fabric);
			}
		}
		//查询面料成分对应的数码印花工艺
		List<Map<String,Object>>  patternPrintMapList=patternPrintService.selectPatternPrintByFfid(fabricFilterList.get(0).getFfId());
		List<PatternPrint> patternPrintList=PatternController.mapChangeToEntity(patternPrintMapList);
		
		if (language != null && language.equals("en")) {
			for (FabricFilter ff : fabricFilterList) {
				ff.setFfName(ff.getFfNameEn());
			}
			for (PatternPrint pp : patternPrintList) {
				pp.setPpName(pp.getPpNameEn());
			}
		}
		
	    Map<String, String> mapUrls = LoadCfgUtil.loadCfg();
		
		model.addAttribute("imgpath",mapUrls.get("targetUrl.img"));		
		model.addAttribute("pattern", pattern);
		model.addAttribute("patternlist", patternlist);
		model.addAttribute("fabricFilterList", fabricFilterList);
		model.addAttribute("fabricListFIngredientsList", fabricListFIngredientsList);
		model.addAttribute("patternPrintList", patternPrintList);
		
		return "phone/fitting/chengpin";
	}
	
	/**
	 * 印花加工页面
	 * @author 张天敏
	 * @date 日期：2016年10月7日 时间：上午10:13:07
	 * @param model
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	@RequestMapping(value="/yinhua",method=RequestMethod.GET)
	public String yinhua(Model model,Integer id,HttpServletRequest request,HttpServletResponse response) throws FileNotFoundException, IOException{
		String language = languageService.getCurrentLanguage();
		
		//查询花型
		Pattern pattern =  patternService.selectByPrimaryKey(id);
		List<Pattern> patternlist =  patternService.findAllBylimit(pattern);
		//查询成分
		//数码印花工艺
		List<PatternPrint>  patternPrintList=patternPrintService.selectAllPrint();
		if (language != null && language.equals("en")) {
			for (PatternPrint pp : patternPrintList) {
				pp.setPpName(pp.getPpNameEn());
			}
		}
		
	    Map<String, String> mapUrls = LoadCfgUtil.loadCfg();
		
		model.addAttribute("imgpath",mapUrls.get("targetUrl.img"));		
		model.addAttribute("pattern", pattern);
		model.addAttribute("patternlist", patternlist);
		model.addAttribute("patternPrintList", patternPrintList);
		return "phone/fitting/yinhua";
	}
	
	/**
	 * 去买断页面
	 * @author 张天敏
	 * @date 日期：2016年10月8日 时间：下午4:53:15
	 * @param model
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	@RequestMapping(value="/maiduan",method=RequestMethod.GET)
	public String maiduan(Model model,Integer id,HttpServletRequest request,HttpServletResponse response) throws FileNotFoundException, IOException{
		//查询花型
		Pattern pattern =  patternService.selectByPrimaryKey(id);
		Map<String, String> mapUrls = LoadCfgUtil.loadCfg();
		model.addAttribute("imgpath",mapUrls.get("targetUrl.img"));		
		HelpCenter helpCenter=helpCenterService.selectDetails(8);//买断花型
		String language = languageService.getCurrentLanguage();
		if (language.equals("en")) {
			helpCenter.sethContent(helpCenter.gethContentEn());
		}
		model.addAttribute("helpCenter", helpCenter);
		model.addAttribute("pattern", pattern);
		return "phone/fitting/maiduan";
	}
	
	/**
	 * 去试衣间
	 * @author 张天敏
	 * @date 日期：2016年10月8日 时间：下午4:53:03
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	@RequestMapping(value = "/goFittingRoom", method = RequestMethod.GET)
	public String goFittingRoom(Model model, HttpServletRequest request, HttpServletResponse response) throws FileNotFoundException, IOException {
		Map<String, String> mapUrls = LoadCfgUtil.loadCfg();
		String imgParent = mapUrls.get("targetUrl.img");
		//从session中获取当前时间
		String language = languageService.getCurrentLanguage();
		List<Fabric> fabricListFIngredientsList = new ArrayList<Fabric>();
		List<Pattern> patternlist = patternService.findDefaultPattern();
		// 查询面料
		List<FabricFilter> fabricFilterList = fabricFilterService.selectByFfParentId(1);
		for (FabricFilter fabricFilter : fabricFilterList) {
			List<Fabric> fabricList = fabricService.selectByFIngredients(fabricFilter.getFfId());
			for (Fabric fabric : fabricList) {
				if (language != null && language.equals("en")) {
					fabric.setfName(fabric.getfMessage1());
				}
				fabricListFIngredientsList.add(fabric);
			}
			//面料帅选条件，如果是系统设定的语言是英语，则将英文字段覆盖中文字段来显示英文
			if (language != null && language.equals("en")) {
				fabricFilter.setFfName(fabricFilter.getFfNameEn());
			}
		}
		// 数码印花工艺
		List<PatternPrint> patternPrintList = patternPrintService.selectAllPrint();
		for(PatternPrint patternPrint:patternPrintList){
			if (language != null && language.equals("en")) {
				patternPrint.setPpName(patternPrint.getPpNameEn());
			}
		}
		model.addAttribute("imgpath", imgParent);
		model.addAttribute("patternlist", patternlist);
		model.addAttribute("fabricFilterList", fabricFilterList);
		model.addAttribute("fabricListFIngredientsList", fabricListFIngredientsList);
		model.addAttribute("patternPrintList", patternPrintList);
		return "phone/pattern/shiyi";
	}
	/**
	 * 我的模特
	 * @author 薛国计
	 * @date 2016年10月9日
	 * @param model
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	@RequestMapping(value="/mote",method=RequestMethod.GET)
	public String mote(Model model,Integer id,HttpServletRequest request,HttpServletResponse response) {
		
		return "phone/fitting/mote";
	}
	
	
}
